//
//  TT_AVPlayer.m
//  goodsDetailBannerDemo
//
//  Created by 王洋 on 2019/7/9.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TT_AVPlayer.h"

@interface TT_AVPlayer () {
    
    BOOL _isPlayerControlShow;
}

@property (nonatomic, copy) NSString *videoUrl;

@property (nonatomic, strong) UIImageView *maskImageView;
@end

@implementation TT_AVPlayer

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        _isPlayerControlShow = NO;
        [self layout];
    }
    return self;
}

- (void)layout {
    
//    TT_PlayerSupreView *playerSuperview = [TT_PlayerSupreView.alloc initWithFrame:CGRectZero];
//    playerSuperview.backgroundColor = UIColor.redColor;
//    [self addSubview:playerSuperview];
//    [playerSuperview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(100);
//        make.left.offset(0);
//        make.width.offset(TTScreenWidth);
//        make.height.equalTo(playerSuperview.mas_width).multipliedBy(9/16.0);
//    }];
    
    [self addSubview:self.txVodPlayer.view];
    [self.txVodPlayer.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self addSubview:self.maskImageView];
    [self addSubview:self.playBtn];
    
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(50);
    }];
    
    [self.maskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    
    [self layoutIfNeeded];
}

- (void)initPlayerWithUrlString:(NSString *)videoUrl AndMaskUrl:(NSString *)maskUrl {

    self.videoUrl = videoUrl;
    
    [self.maskImageView sd_setImageWithURL:[NSURL URLWithString:maskUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];

    [self bringSubviewToFront:self.playBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePlayerControlStatus)];
    [self addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
//    [self.item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
}

#pragma mark 播放控制的显示与隐藏
- (void)changePlayerControlStatus {
    _isPlayerControlShow = !_isPlayerControlShow;
    if (_isPlayerControlShow) {
        [self showPlayControl];
    } else {
        [self hidePlayControl];
    }
}

- (void)showPlayControl {
    self.playBtn.hidden = NO;
    _isPlayerControlShow = YES;
}

- (void)hidePlayControl {
    self.playBtn.hidden = YES;
    _isPlayerControlShow = NO;
}

#pragma mark 暂停：1、player开启   2、播放控制隐藏    
- (void)play {

    [self.txVodPlayer vc_viewDidAppear];

    if (self.maskImageView) {
        [self.maskImageView removeFromSuperview];
    }

    self.txVodPlayer.URLAsset = [SJVideoPlayerURLAsset.alloc initWithURL:[NSURL URLWithString:self.videoUrl]];

    [self.txVodPlayer play];
    [self hidePlayControl];
}

#pragma mark 暂停：1、player暂停   2、播放控制显示    3、播放按钮状态变更为可播放
- (void)pause {
//    [self.player pause];
    
    [self.txVodPlayer pause];
    [self showPlayControl];
    self.playBtn.selected = NO;
}

-(void)clearCache {
    
//    [self.txVodPlayer ];
}

- (void)btnOnClicked:(UIButton *)btn {

    btn.selected = !btn.selected;
    if (self.txVodPlayer.isPlaying) {
        [self pause];
    } else {
        [self play];
    }
}

#pragma mark 视频播放结束
- (void)videoPlayEnd:(NSNotification *)notic
{
    if (!self.txVodPlayer) {
        return;
    }
    [self.playBtn setSelected:NO];
    [self.playBtn setHidden:NO];
    self.isEndPlay = YES;
}

- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _playBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_playBtn setImage:[UIImage imageNamed:@"play_f1f1f1"] forState:UIControlStateNormal];
        [_playBtn setImage:[UIImage imageNamed:@"pause_f1f1f1"] forState:UIControlStateSelected];
        [_playBtn addTarget:self action:@selector(btnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

- (SJVideoPlayer *)txVodPlayer {
    if (!_txVodPlayer) {
        _txVodPlayer = SJVideoPlayer.player;
        _txVodPlayer.gestureController.supportedGestureTypes = SJPlayerGestureTypeMask_None;
    }
    return _txVodPlayer;
}

- (UIImageView *)maskImageView {
    
    if (!_maskImageView) {
        _maskImageView = [[UIImageView alloc] init];
        _maskImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _maskImageView;
}

@end
